//////////////////////////////////////////////////////////////
// TITLE: 			Split Annual Files
//
// DESCRIPTION:
// Take each year HD file and split into 100 groups per year, save each group-year file. 
//////////////////////////////////////////////////////////////

local year = `1' + 2005 + - 1

cap mkdir "$reconstruct/`year'"

use "$morningstar_hd/HD_`year'_m.dta"

local sleeptime =((`year'-2005)*5000)
sleep `sleeptime'
* Sleep command avoids IO error on overusing this merge
cap drop _merge
merge m:1 MasterPortfolioId using "$reconstruct/mpid_list.dta"
drop if _merge==2
drop _merge

levelsof group, local(levels)

preserve
foreach i of local levels {
    keep if group == `i'
    save "$reconstruct/`year'/HD_`year'_part_`i'", replace emptyok
    restore, preserve 
}
